package com.culturesalon.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.culturesalon.dao.CategoryDAO;
import com.culturesalon.dao.CategoryDAOImpl;
import com.culturesalon.dao.GoodsDAO;
import com.culturesalon.dao.GoodsDAOImpl;
import com.culturesalon.dto.Category;
import com.culturesalon.dto.Goods;
import com.culturesalon.dto.Page;
import com.culturesalon.util.NaviUtil;
import com.culturesalon.util.PageUtil;
import com.culturesalon.util.StringUtil;

/**
 * Servlet implementation class GoodsList
 */
@WebServlet("/goodslist.do")
public class GoodsList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GoodsList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String categoryId=request.getParameter("categoryId");
		String currentPage=request.getParameter("currentPage");
		Category category=null;
		if(StringUtil.isEmpty(currentPage)){
			currentPage="1";
		}
		if(!StringUtil.isEmpty(categoryId)){
			CategoryDAO categoryDAO=new CategoryDAOImpl();
			
			category=categoryDAO.get(categoryId);
			String naviResult=NaviUtil.genGoodsListNavigation(category.getCategoryName(), categoryId);
			request.setAttribute("naviResult", naviResult);
			//category.setId(Integer.parseInt(categoryId));
		}
		Page page=new Page(Integer.parseInt(currentPage),8);
		GoodsDAO goodsDAO=new GoodsDAOImpl();
		int total=goodsDAO.getCount(category);
		//String pagation=PageUtil.getUpAndDownPagation(total, Integer.parseInt(currentPage), page.getPageSize(), category.getId());
		//request.setAttribute("pagation", pagation);
		List<Goods> listByCategory=goodsDAO.findAll(page, category);
		request.setAttribute("listByCategory", listByCategory);
		request.setAttribute("goods", listByCategory);
		request.setAttribute("mainpage", "listgoods.jsp");
		request.getRequestDispatcher("admin/adminindex.jsp").forward(request, response);
		
	}

}
